package com.songlanyun.modules.loverecord.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.IntegralConstant;
import com.songlanyun.common.enums.LoveConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.account.entity.UserIdentityAssetEntity;
import com.songlanyun.modules.account.service.UserIdentityAssetService;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.integralrecord.entity.IntegralRecord;
import com.songlanyun.modules.integralrecord.service.IntegralRecordService;
import com.songlanyun.modules.loverecord.dao.UserLoveRecordDao;
import com.songlanyun.modules.loverecord.entity.UserLoveRecordEntity;
import com.songlanyun.modules.loverecord.service.UserLoveRecordService;
import com.songlanyun.modules.pointsexchangeconfig.entity.PointsExchangeConfigEntity;
import com.songlanyun.modules.pointsexchangeconfig.service.PointsExchangeConfigService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("userLoveRecordService")
public class UserLoveRecordServiceImpl extends ServiceImpl<UserLoveRecordDao, UserLoveRecordEntity> implements UserLoveRecordService {

    @Resource
    private UserIdentityAssetService userIdentityAssetService;

    @Resource
    private PointsExchangeConfigService pointsExchangeConfigService;

    @Resource
    private IntegralRecordService integralRecordService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long userId = MapUtil.getLong(params, "user_id");
        IPage<UserLoveRecordEntity> page = this.page(
                new Query<UserLoveRecordEntity>().getPage(params),
                new LambdaQueryWrapper<UserLoveRecordEntity>()
                        .eq(ObjectUtil.isNotNull(userId), UserLoveRecordEntity::getUserId, userId)
                        .orderByDesc(UserLoveRecordEntity::getCreateTime)
        );

        return new PageUtils(page);
    }

    @Override
    public UserLoveRecordEntity add(Long userId, Boolean billType, Integer value, LoveConstant.ModelType modelType, Long modelId, String remark) {
        if (value <= 0) {//爱心为0时，无需进行操作
            throw new RRException(MemberException.TOO_LITTLE_LOVE_IN_EXCHANGE);
        }
        userIdentityAssetService.updateLoveValue(userId, value, billType);

        //保存爱心收支记录
        UserLoveRecordEntity entity = new UserLoveRecordEntity();
        entity.setBillType(billType);
        entity.setValue(value);
        entity.setUserId(userId);
        entity.setModelType(modelType);
        entity.setModelId(modelId);
        entity.setRemark(remark);
        this.baseMapper.insert(entity);

        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void loveValueExchange(Long userId, Integer loveValue) {
        //保存爱心记录及更新用户爱心
        UserLoveRecordEntity userLoveRecordEntity = add(userId, false, loveValue, LoveConstant.ModelType.LOVE_VALUE_EXCHANGE, null, "爱心值兑换公益积分");

        //兑换的积分值
        PointsExchangeConfigEntity config = pointsExchangeConfigService.loadConfig(true);
        BigDecimal integral = MathUtils.multiply(MathUtils.divide(BigDecimal.valueOf(loveValue), BigDecimal.valueOf(config.getLoveValue())), config.getPublicWelfareIntegral());
        if (integral.compareTo(BigDecimal.ZERO) <= 0) {//积分为0时，无需进行操作
            throw new RRException(MemberException.TOO_FEW_POINTS_AFTER_HEART_VALUE_EXCHANGE);
        }

//        //增加用户公益积分
//        IntegralRecord integralRecord = new IntegralRecord(userId, integral, IntegralConstant.InnerMode.LOVE_VALUE_EXCHANGE,
//                IntegralConstant.IntegralModelType.LOVE_VALUE_EXCHANGE, userLoveRecordEntity.getId(), "爱心值兑换公益积分");
//        integralRecordService.inner(integralRecord);
    }
}
